define([
    'jquery',
    'underscore',
    'backbone',
    'views/business/item',
    'iscroll'
], function ($, _, Backbone, BusinessUpdateView) {
    var BusinessForm = Backbone.View.extend({

        tagName:"div", // Not required since 'div' is the default if no el or tagName specified

        initialize:function () {

            this.template = _.template(tpl.get('business/business-update'));
            this.model.bind("change", this.render, this);
        },

        render:function (eventName) {
            $(this.el).html(this.template(this.model.toJSON()));
            return this;
        },

        events:{
            "change input":"change",
            "click .save":"saveBusiness",
            "click .delete":"deleteBusiness"
        },

        change:function (event) {
            var target = event.target;
            console.log('changing ' + target.id + ' from: ' + target.defaultValue + ' to: ' + target.value);
            // You could change your model on the spot, like this:
            // var change = {};
            // change[target.name] = target.value;
            // this.model.set(change);
        },

        saveBusiness:function () {
            this.model.set({
                name:$('#name').val(),
                overview:$('#overview').val(),
                bio:$('#bio').val()
            });

            var self = this;
            if (this.model.isNew()) {
                app.businessList.create(this.model, {
                    success:function () {
                        app.businessDetails(self.model.id);
                        app.navigate('business/' + self.model.id);
                    }
                });
            } else {
                this.model.save(this.model, {
                    success:function () {
                        app.businessDetails(self.model.id);
                        app.navigate('business/' + self.model.id);
                    }
                });
            }

            return false;
        },

        deleteBusiness:function () {
            this.model.destroy({
                success:function () {
                    alert('Business deleted successfully');
                    app.list();
                    app.navigate('business/new');
                }
            });
            return false;
        }

    });
});